package com.giscloud.amp.service.sys.impl;

import com.giscloud.amp.dao.PubDictMapper;
import com.giscloud.amp.dao.PubDictValueMapper;
import com.giscloud.amp.entity.PubDict;
import com.giscloud.amp.entity.PubDictExample;
import com.giscloud.amp.entity.PubDictValue;
import com.giscloud.amp.entity.PubDictValueExample;
import com.giscloud.amp.entity.dto.DictionaryDTO;
import com.giscloud.amp.entity.dto.DictionaryValueDTO;
import com.giscloud.amp.service.sys.DictionaryService;

import com.giscloud.commons.model.Result;
import com.giscloud.datasource.page.PageInfo;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author gxd
 * @data 2018/12/4 10:53
 */
@Service("dictionaryService")
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private PubDictMapper pubDictMapper;

    @Autowired
    private PubDictValueMapper pubDictValueMapper;


    @Override
    public PageInfo getList(DictionaryDTO dictionaryDTO) {
        PageHelper.startPage(dictionaryDTO.getPage(), dictionaryDTO.getRows());
        PubDictExample pubDictExample = new PubDictExample();
        PubDictExample.Criteria criteria = pubDictExample.createCriteria();
        if(StringUtils.isNotEmpty(dictionaryDTO.getName())){
            criteria.andNameLike("%"+dictionaryDTO.getName()+"%");
        }
        pubDictExample.setOrderByClause("created_dt desc");
        List<PubDict> list = pubDictMapper.selectByExample(pubDictExample);
        return PageInfo.of(list);
    }

    @Override
    public Result add(DictionaryDTO dictionaryDTO) {
        PubDict pubDict = new PubDict();
        pubDict.setCreatedDt(new Date());
        pubDict.setDictKey(dictionaryDTO.getDictKey());
        pubDict.setName(dictionaryDTO.getName());
        pubDict.setRemark(dictionaryDTO.getRemark());
        pubDictMapper.insertSelective(pubDict);
        return Result.success("添加成功");
    }

    @Override
    public Result delete(Long dictId) {//字典删除，应该删除旗下的字典值
        int i = pubDictMapper.deleteByPrimaryKey(dictId);
        if(i>0){
            PubDictValueExample pubDictValueExample = new PubDictValueExample();
            PubDictValueExample.Criteria criteria = pubDictValueExample.createCriteria();
            criteria.andDictIdEqualTo(dictId);
            pubDictValueMapper.deleteByExample(pubDictValueExample);
        }
        return Result.success("删除成功");
    }

    @Override
    public Result edit(DictionaryDTO dictionaryDTO) {

        PubDict pubDict = new PubDict();
        pubDict.setDictId(dictionaryDTO.getDictId());
        pubDict.setRemark(dictionaryDTO.getRemark());
        pubDict.setName(dictionaryDTO.getName());
        pubDict.setVersion(new Date());
        pubDictMapper.updateByPrimaryKeySelective(pubDict);

        return Result.success("修改成功");
    }

    @Override
    public PageInfo getListValue(DictionaryValueDTO dictionaryValueDTO) {
        PageHelper.startPage(dictionaryValueDTO.getPage(), dictionaryValueDTO.getRows());
        PubDictValueExample pubDictValueExample = new PubDictValueExample();
        PubDictValueExample.Criteria criteria = pubDictValueExample.createCriteria();
        pubDictValueExample.setOrderByClause("disp_order");
        if(StringUtils.isNotEmpty(dictionaryValueDTO.getName())){
            criteria.andNameLike("%"+dictionaryValueDTO.getName()+"%");
        }
        if(dictionaryValueDTO.getDictId()!=null){
            criteria.andDictIdEqualTo(Long.parseLong(dictionaryValueDTO.getDictId()));
        }
        if(StringUtils.isNotBlank(dictionaryValueDTO.getDictKey())){
            criteria.andDictKeyEqualTo(dictionaryValueDTO.getDictKey());
        }
        List<PubDictValue> list = pubDictValueMapper.selectByExample(pubDictValueExample);
        return PageInfo.of(list);
    }

    @Override
    public Result valueAdd(DictionaryValueDTO dictionaryValueDTO) {
        PubDictValue pubDictValue = new PubDictValue();
        pubDictValue.setCreatedDt(new Date());
        pubDictValue.setCode(Integer.valueOf(dictionaryValueDTO.getCode()));
        pubDictValue.setName(dictionaryValueDTO.getName());
        pubDictValue.setRemark(dictionaryValueDTO.getRemark());
        pubDictValue.setDispOrder(dictionaryValueDTO.getDispOrder());
        pubDictValue.setDictId(Long.parseLong(dictionaryValueDTO.getDictId()));
        PubDict pubDict  = pubDictMapper.selectByPrimaryKey(Long.parseLong(dictionaryValueDTO.getDictId()));
        pubDictValue.setDictKey(pubDict.getDictKey());
        pubDictValueMapper.insertSelective(pubDictValue);
        return Result.success("添加成功");
    }

    @Override
    public Result valueDelete(Long dictValueId) {
        pubDictValueMapper.deleteByPrimaryKey(dictValueId);
        return Result.success("删除成功");
    }

    @Override
    public Result valueEdit(DictionaryValueDTO dictionaryValueDTO) {

        PubDictValue pubDictValue = new PubDictValue();
        pubDictValue.setDictValueId(dictionaryValueDTO.getDictValueId());
        pubDictValue.setCode(Integer.valueOf(dictionaryValueDTO.getCode()));
        pubDictValue.setName(dictionaryValueDTO.getName());
        pubDictValue.setRemark(dictionaryValueDTO.getRemark());
        pubDictValue.setVersion(new Date());
        pubDictValue.setDictId(Long.parseLong(dictionaryValueDTO.getDictId()));
        pubDictValue.setDispOrder(dictionaryValueDTO.getDispOrder());
        PubDict pubDict  = pubDictMapper.selectByPrimaryKey(Long.parseLong(dictionaryValueDTO.getDictId()));
        pubDictValue.setDictKey(pubDict.getDictKey());
        pubDictValueMapper.updateByPrimaryKeySelective(pubDictValue);
        return Result.success("编辑成功");
    }

    @Override
    public  List<Map<String,Object>> getDictMap() {
        List<Map<String,Object>> map = pubDictMapper.getList();
        return map;
    }

    @Override
    public List<Map<String, Object>> getDictList(String dictKey) {
        return pubDictValueMapper.getList(dictKey);
    }

    @Override
    public List<Map<String,Object>> pubdictValueList(DictionaryValueDTO dictionaryValueDTO) {
        PageHelper.startPage(dictionaryValueDTO.getPage(),200);
        PubDictValueExample pubDictValueExample = new PubDictValueExample();
        PubDictValueExample.Criteria criteria = pubDictValueExample.createCriteria();
        criteria.andDictKeyEqualTo(dictionaryValueDTO.getDictKey());
        if(StringUtils.isNotBlank(dictionaryValueDTO.getName())){
            criteria.andNameLike("%"+dictionaryValueDTO.getName()+"%");
        }
        pubDictValueExample.setOrderByClause("disp_order");
        List<Map<String,Object>> pubDictValues = pubDictValueMapper.pubdictValueList(pubDictValueExample);
        return pubDictValues;
    }
}
